package com.huihui.network;

import com.huihui.network.base.CallBackUtil;
import com.huihui.network.base.OkhttpUtil;
import com.huihui.network.base.RequestUtil;
import com.huihui.utils.StringUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by daimeng on 2018/8/8 11:05.
 */

public class OkHttpRequest {
    private OkHttpRequest() {}

    private static OkHttpRequest single = null;

    public static OkHttpRequest getInstance() {
        synchronized(OkHttpRequest.class){
            // 注意：里面的判断是一定要加的，否则出现线程安全问题
            if(single == null){
                single = new OkHttpRequest();
            }
        }
        return single;
    }

    /*----------------用户板块-----------------------*/

    /**
     * 用户注册
     * @param url
     * @param telphone
     * @param sms_code
     * @param callBackUtil
     */
    public void register(String url, String telphone, String sms_code, CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("telphone",telphone);
        paramsMap.put("sms_code",sms_code);
        OkhttpUtil.okHttpPost(url, paramsMap, callBackUtil);
    }

    /**
     * 发送手机短信验证码
     * @param url
     * @param user_id
     * @param telphone
     * @param sms_type 0 登录  1 注册  2 修改密码
     * @param callBackUtil
     */
    public void sendSmsCode(String url,long user_id,String telphone,int sms_type,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("telphone",telphone);
        paramsMap.put("sms_type",String.valueOf(sms_type));
        OkhttpUtil.okHttpPost(url, paramsMap, callBackUtil);
    }

    /**
     * 绑定手机号-提交
     * @param url
     * @param user_id
     * @param telphone
     * @param sms_code
     * @param callBackUtil
     */
    public void bindTelhone(String url,long user_id,String telphone,String sms_code,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("telphone",telphone);
        paramsMap.put("sms_code",sms_code);
        OkhttpUtil.okHttpPost(url, paramsMap, callBackUtil);
    }

    /**
     *用户登录（账号密码/手机验证码） -F
     * @param url
     * @param telphone
     * @param password
     * @param sms_code
     * @param login_type 登录类型  1:手机注册，2:微信注册，3:微博注册
     * @param callBackUtil
     */
    public void login(String url,String telphone,String password,String sms_code,int login_type,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("telphone",telphone);
        paramsMap.put("password",password);
        paramsMap.put("sms_code",sms_code);
        paramsMap.put("login_type",String.valueOf(login_type));
        OkhttpUtil.okHttpPost(url, paramsMap, callBackUtil);
    }

    public void forgetPsw(String url,String telphone,String sms_code,String password,String confirm_password,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("telphone",telphone);
        paramsMap.put("sms_code",sms_code);
        paramsMap.put("password",password);
        paramsMap.put("confirm_password",confirm_password);
        OkhttpUtil.okHttpPost(url, paramsMap, callBackUtil);
    }

    public void doReport(String url,long report_user_id,long beReport_user_id,String report_details,long current_report_content_id,int report_type,int class_id,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("report_user_id",String.valueOf(report_user_id));
        paramsMap.put("beReport_user_id",String.valueOf(beReport_user_id));
        paramsMap.put("report_details",report_details);
        paramsMap.put("current_report_content_id",String.valueOf(current_report_content_id));
        paramsMap.put("report_type",String.valueOf(report_type));
        paramsMap.put("class_id",String.valueOf(class_id));
        OkhttpUtil.okHttpPost(url, paramsMap, callBackUtil);
    }

    /**
     *第三方授权登录后保存用户信息
     * @param url
     * @param unionid
     * @param nick_name
     * @param head_img_url
     * @param sex （0女；1男）
     * @param callBackUtil
     */
    public void saveUserByPlatform(String url,String unionid,String nick_name,String head_img_url,int sex,int register_type,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("unionid",unionid);
        paramsMap.put("nick_name",nick_name);
        paramsMap.put("head_img_url",head_img_url);
        paramsMap.put("sex",String.valueOf(sex));
        paramsMap.put("register_type",String.valueOf(register_type));
        OkhttpUtil.okHttpPost(url, paramsMap, callBackUtil);
    }

    public void getWbInfo(String appkey,String token, String uid, final CallBackUtil callBackUtil){
        String url = "https://api.weibo.com/2/users/show.json?appkey=APPKEY&access_token=ACCESS_TOKEN&uid=UID";
        String url2 = url.replaceAll("APPKEY",appkey);
        String url3 = url2.replaceAll("ACCESS_TOKEN",token);
        String urlGet = url3.replaceAll("UID",uid);
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(urlGet)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(callBackUtil != null){
                    if (callBackUtil != null) {
                        callBackUtil.onSeccess(call, response);
                    }
                }

            }
        });
    }

    public void getAccessToeknWeixin(String code, final CallBackUtil callBackUtil){
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9fb11f4d733cefb8&secret=e62f1cd494bc63aff90723c64b2988f9&code=CODE&grant_type=authorization_code";
        String urlGet = url.replaceAll("CODE",code);
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(urlGet)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(callBackUtil != null){
                    if (callBackUtil != null) {
                        callBackUtil.onSeccess(call, response);
                    }
                }

            }
        });
    }

    public void getWeixinInfo(String access_token, String openid, final CallBackUtil callBackUtil){
        String url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
        String url1 = url.replaceAll("ACCESS_TOKEN",access_token);
        String urlGet = url1.replaceAll("OPENID",openid);
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(urlGet)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(callBackUtil != null){
                    if (callBackUtil != null) {
                        callBackUtil.onSeccess(call, response);
                    }
                }

            }
        });
    }
    /**
     * 第三方平台登录获取用户信息
     * @param url
     * @param unionid
     * @param callBackUtil
     */
    public void getUserByUnionid(String url,String unionid,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("unionid",unionid);
        OkhttpUtil.okHttpPost(url, paramsMap, callBackUtil);
    }

    /**
     * 重置密码
     * @param url
     * @param user_id
     * @param telphone
     * @param sms_code
     * @param password
     * @param confirm_password
     * @param reset_type (0忘记密码通过短信方式重置；1已登录成功直接修改密码)
     * @param callBackUtil
     */
    public void resetPassword(String url,long user_id,String telphone,String sms_code,String password,String confirm_password,int reset_type,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
//        paramsMap.put("telphone",telphone);
//        paramsMap.put("sms_code",sms_code);
        paramsMap.put("password",password);
        paramsMap.put("confirm_password",confirm_password);
        paramsMap.put("reset_type",String.valueOf(reset_type));
        OkhttpUtil.okHttpPost(url, paramsMap, callBackUtil);
    }


    /**
     * 更新用户信息
     * @param url
     * @param user_id
     * @param sex
     * @param nick_name
     * @param callBackUtil
     */
    public void updateUser(String url,long user_id,int sex,String nick_name,File file,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("sex",String.valueOf(sex));
        paramsMap.put("nick_name",nick_name);
//        paramsMap.put("img_file_name",img_file_name);
//        OkhttpUtil.okHttpPost(url, paramsMap,callBackUtil);
        OkhttpUtil.okHttpUploadFile(url,file,"file","image",paramsMap,callBackUtil);
    }

    /**
     * 我的点赞或者收藏
     * @param url
     * @param user_id
     * @param page_size
     * @param current_page
     * @param callBackUtil
     */
    public void mineList(String url,long user_id,int page_size,int current_page,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("page_size",String.valueOf(page_size));
        paramsMap.put("current_page",String.valueOf(current_page));
//        OkhttpUtil.okHttpPost(url, paramsMap, callBackUtil);
        OkhttpUtil.okHttpGet(url, paramsMap, callBackUtil);
    }

    /**
     * 我的消息-列表
     * @param url
     * @param user_id
     * @param msg_type
     * @param page_size
     * @param current_page
     * @param callBackUtil
     */
    public void msgList(String url,long user_id,int msg_type,int page_size,int current_page,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("msg_type",String.valueOf(msg_type));
        paramsMap.put("page_size",String.valueOf(page_size));
        paramsMap.put("current_page",String.valueOf(current_page));
        OkhttpUtil.okHttpGet(url, paramsMap, callBackUtil);
    }

    /**
     * 修改信息为已读
     * @param url
     * @param msg_id
     * @param callBackUtil
     */
    public void defineMsgRead(String url,int msg_id,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("msg_id",String.valueOf(msg_id));
        OkhttpUtil.okHttpPost(url, paramsMap, callBackUtil);
    }

    /**
     * 用户反馈
     * @param url
     * @param user_id
     * @param content
     * @param contact_info
     * @param callBackUtil
     */
    public void uploadFeedback(String url, long user_id, String content,  String contact_info,String attach_files_name, CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("content",content);
        paramsMap.put("contact_info",contact_info);
        paramsMap.put("attach_files_name",attach_files_name);
        OkhttpUtil.okHttpPost(url, paramsMap,callBackUtil);
    }

    /*-------------------------------业务板块-------------------------------------*/

    /**
     *
     * @param url
     * @param area_id 0超值，1最新
     * @param current_page
     * @param callBackUtil
     */
    public void getProductList(String url,int area_id,int page_size,int current_page,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("area_id",String.valueOf(area_id));
        paramsMap.put("page_size",String.valueOf(page_size));
        paramsMap.put("current_page",String.valueOf(current_page));
        OkhttpUtil.okHttpGet(url, paramsMap, callBackUtil);
    }

    /**
     * 获取商品评论列表
     * @param url
     * @param prod_id 商品id
     * @param page_size
     * @param current_page
     * @param callBackUtil
     */
    public void getProductComment(String url,long prod_id,int page_size,int current_page,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("prod_id",String.valueOf(prod_id));
        paramsMap.put("page_size",String.valueOf(page_size));
        paramsMap.put("current_page",String.valueOf(current_page));
        OkhttpUtil.okHttpGet(url, paramsMap, callBackUtil);
    }

    public void getArticleComment(String url,long art_id,int page_size,int current_page,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("art_id",String.valueOf(art_id));
        paramsMap.put("page_size",String.valueOf(page_size));
        paramsMap.put("current_page",String.valueOf(current_page));
        OkhttpUtil.okHttpGet(url, paramsMap, callBackUtil);
    }
    public void getVideoComment(String url,long video_id,int page_size,int current_page,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("video_id",String.valueOf(video_id));
        paramsMap.put("page_size",String.valueOf(page_size));
        paramsMap.put("current_page",String.valueOf(current_page));
        OkhttpUtil.okHttpGet(url, paramsMap, callBackUtil);
    }
    public void getChildComment(String url,long relation_id,int class_id,long parent_id,int page_size,int current_page,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("relation_id",String.valueOf(relation_id));
        paramsMap.put("class_id",String.valueOf(class_id));
        paramsMap.put("parent_id",String.valueOf(parent_id));
        paramsMap.put("page_size",String.valueOf(page_size));
        paramsMap.put("current_page",String.valueOf(current_page));
        OkhttpUtil.okHttpGet(url, paramsMap, callBackUtil);
    }
    /**
     * 添加评论
     * @param url
     * @param pro_id 商品id
     * @param parentCommentId 父评论的id  不存在就传0
     * @param callBackUtil
     */
    public void addProductComment(String url,long user_id,long pro_id,int parentCommentId,String comment,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("product_id",String.valueOf(pro_id));
        paramsMap.put("parent_comment_id",String.valueOf(parentCommentId));
        paramsMap.put("comment",comment);
        OkhttpUtil.okHttpPost(url, paramsMap,callBackUtil);
    }

    public void addArticleComment(String url,long user_id,long art_id,long parent_comment_id,String comment,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("art_id",String.valueOf(art_id));
        paramsMap.put("parent_comment_id",String.valueOf(parent_comment_id));
        paramsMap.put("comment",comment);
        OkhttpUtil.okHttpPost(url, paramsMap,callBackUtil);
    }

    public void addVideoComment(String url,long user_id,long video_id,long parent_comment_id,String comment,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("video_id",String.valueOf(video_id));
        paramsMap.put("parent_comment_id",String.valueOf(parent_comment_id));
        paramsMap.put("comment",comment);
        OkhttpUtil.okHttpPost(url, paramsMap,callBackUtil);
    }
    /**
     * 加入收藏/点赞  -F
     * @param url
     * @param user_id
     * @param relation_id 	关联的内容ID
     * @param class_id 内容类型（0商品；1视频；2文章）
     * @param relation_comment_id 点赞的评论ID（如果是内容点赞此处为0）
     * @param callBackUtil
     */
    public void addCollection(String url,long user_id,long relation_id,int class_id,int relation_comment_id,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("relation_id",String.valueOf(relation_id));
        paramsMap.put("class_id",String.valueOf(class_id));
        paramsMap.put("relation_comment_id",String.valueOf(relation_comment_id));
        OkhttpUtil.okHttpPost(url, paramsMap,callBackUtil);
    }
    public void removeCollection(String url,long user_id,long relation_id,int class_id,int commemt_id,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("relation_id",String.valueOf(relation_id));
        paramsMap.put("class_id",String.valueOf(class_id));
        paramsMap.put("commemt_id",String.valueOf(commemt_id));
        OkhttpUtil.okHttpPost(url, paramsMap,callBackUtil);
    }
    public static void getShareUrl(String url,long user_id,int class_id,long relation_id,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("class_id",String.valueOf(class_id));
        paramsMap.put("relation_id",String.valueOf(relation_id));
        OkhttpUtil.okHttpGet(url, paramsMap,callBackUtil);

    }
    /**
    /**
     * 最新/超值-搜索 -F
     * @param url
     * @param keyword
     * @param page_size
     * @param current_page
     * @param callBackUtil
     */
    public void productSearch(String url,String keyword,int page_size,int current_page,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("keywords",keyword);
        paramsMap.put("page_size",String.valueOf(page_size));
        paramsMap.put("current_page",String.valueOf(current_page));
        OkhttpUtil.okHttpGet(url, paramsMap, callBackUtil);
    }

    /**
     * 获取视频列表（带搜索）
     * @param url
     * @param keywords
     * @param page_size
     * @param current_page
     * @param callBackUtil
     */
    public void videoList(String url,String keywords,int page_size,int current_page,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("keywords",keywords);
        paramsMap.put("page_size",String.valueOf(page_size));
        paramsMap.put("current_page",String.valueOf(current_page));
        OkhttpUtil.okHttpGet(url, paramsMap, callBackUtil);
    }

    public void articleList(String url,String keywords,int page_size,int current_page,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("keywords",keywords);
        paramsMap.put("page_size",String.valueOf(page_size));
        paramsMap.put("current_page",String.valueOf(current_page));
        OkhttpUtil.okHttpGet(url, paramsMap, callBackUtil);
    }
    public void getVideoInfo(String url,long video_id,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("video_id",String.valueOf(video_id));
        OkhttpUtil.okHttpGet(url, paramsMap, callBackUtil);
    }





    public void publishArticle(String url, long user_id, String content, String attach_files_name,CallBackUtil callBackUtil){
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("user_id",String.valueOf(user_id));
        paramsMap.put("content",content);
        paramsMap.put("attach_files_name",attach_files_name);
        OkhttpUtil.okHttpPost(url, paramsMap,callBackUtil);
    }


    public void getStsToken(String url,CallBackUtil callBackUtil){
        OkhttpUtil.okHttpGet(url,callBackUtil);
    }


}
